package com.sixbro.shiro.session.controller;

import com.bxs.rbac.shiro.session.common.BaseController;
import com.bxs.rbac.shiro.session.common.BaseRequest;
import com.bxs.rbac.shiro.session.common.BaseResponse;
import com.bxs.rbac.shiro.session.domain.entity.User;
import com.bxs.rbac.shiro.session.service.UserService;
import com.bxs.rbac.shiro.session.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/17 16:14
 */
@Controller
public class LoginController extends BaseController {
    private static final Logger _logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;
    @Resource
    private RedisTemplate<String, User> template;

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "redirect:/index";
    }

    /**
     * 登录页(shiro配置需要两个/login 接口,一个是get用来获取登陆页面,一个用post用于登录)
     * @return
     */
    @GetMapping(value = "/login")
    public String login() {
        if (ShiroUtils.isAuthenticated()) {
            return "redirect:/index";
        }
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestHeader(name="Content-Type", defaultValue = "application/json") String contentType, User user) throws Exception{
        _logger.info("用户请求登录获取Token");
        _logger.info(ShiroUtils.md5(user.getPassword(), user).toUpperCase());

        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), ShiroUtils.md5(user.getPassword(), user).toUpperCase());

        login(token);

        user = getCurrentUser();

        return "redirect:/index";
    }

    @ResponseBody
    @PostMapping("/signup")
    public BaseResponse signup(@RequestBody BaseRequest request){
        _logger.info("用户注册");

        User user = request.getJSONObject("user").toJavaObject(User.class);
        String salt = user.getPassword() + ":" + user.getUsername();

        return null;
    }

    /**
     * 未授权
     * @return
     */
    @GetMapping("/unauth")
    public String unauth(){
        if(!SecurityUtils.getSubject().isAuthenticated()){
            return "redirect:/login";
        }
        return "unauth";
    }

    /**
     * 退出
     * @return
     */
    @ResponseBody
    @PostMapping("/logout")
    public Object logout(){
        _logger.info("退出");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new BaseResponse("200","退出成功",null);
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping("/index")
    public String index(ModelMap map){
        _logger.info("主页展示");
        return "index";
    }
}
